﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using HeqinSjsc2024.Class.Models.DtoDing;
using Microsoft.SqlServer.Server;

namespace HeqinSjsc2024.Data
{
    public class AppDbContext : DbContext
    {
        internal DbSet<DtoDingDepList> DtoDingDepLists { get; set; }
        //public DbSet

        public AppDbContext() : base(frmMain._SqlConTxt) // 用于指定连接字符串名称
        {

        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<DtoDingDepList>().ToTable("DT_DepList");
            //   modelBuilder.Entity<Employee>().ToTable("DT_Employee");
        }
    }

    public class DtoDingDepListService
    {
        #region DtoDingDepList部门CRUD

        //添加新部门
        internal void AddDepartment(DtoDingDepList dtoDingDepList)
        {
            using (var context = new AppDbContext())
            {
                context.DtoDingDepLists.Add(dtoDingDepList);
                context.SaveChanges();
            }
        }
        //获取所有部门
        internal List<DtoDingDepList> GetAllDepartments()
        {
            using (var context = new AppDbContext())
            {
                return context.DtoDingDepLists.ToList();
            }
        }
        //根据ID得到部门
        internal DtoDingDepList GetDepartmentByDeptId(long deptId)
        {
            using (var context = new AppDbContext())
            {
                return context.DtoDingDepLists.Find(deptId);
            }
        }
        //更新部门
        internal void UpdateDepartment(DtoDingDepList dtoDingDepList)
        {
            using (var context = new AppDbContext())
            {
                context.Entry(dtoDingDepList).State = EntityState.Modified;
                context.SaveChanges();
            }
        }
        //删除部门
        internal void DeleteDepartment(long deptId)
        {
            using (var context = new AppDbContext())
            {
                var department = context.DtoDingDepLists.Find(deptId);
                if (department != null)
                {
                    context.DtoDingDepLists.Remove(department);
                    context.SaveChanges();
                }
            }
        }

        #endregion DtoDingDepList部门CRUD结束

    }









}

